#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
int main()
{
	double a = 0, c = 0, cb = 0;
	long int b = 0;
	int tmp1 = 0, tmp2 = 0, btime = 0, etime = 0;
	srand((int)time(0));
	for (;;) {
		printf("请输入预定概率\n");
		scanf("%lf", &a);
		if (a == 0.0 || a > 1.0 || a < 0.0) {
			printf("请重新输入\n\n");
		} else {
			break;
		}
	}
	for (;;) {
		printf("请输入实验次数\n");
		scanf("%ld", &b);
		if (b <= 0) {
			printf("请重新输入\n\n");
		}  else {
			break;
		}
	}
	
	btime = clock();
	FILE *fp;
	fp = fopen("2.xls", "w");
	for (tmp1 = 1; tmp1 <= b; tmp1++)
	{
		if (tmp1 < 20)
		{
		eeew: 
			c = (a - (rand() / 32768.0));
			if (c > 1.0) goto eeew;
			if (c < a - (0.1 * a)) goto eeew;
			fprintf(fp,"%lf\n",c);			
		}
		else if (tmp1 < (b / 2.0))
		{
			c = (a - (rand() / 655350.0));			
			fprintf(fp,"%lf\n",c);

		}
		else if (tmp1 < (b * 0.75))
		{
			c = (a + (rand() / 3276800.0));		
			fprintf(fp,"%lf\n",c);
		}
		else if (tmp1 < (b * 0.9))
		{
			c = (a - (rand() / 3276800.0));			
			fprintf(fp,"%lf\n",c);
		}
		else if (tmp1 < (b * 0.99))
		{
			c = (a + (rand() / 32768000.0));		
			fprintf(fp,"%lf\n",c);
		}
		else {
			c = (a + (rand() / 327680000.0));
			fprintf(fp,"%lf\n",c);
		}
		cb = ((double)tmp1/(double)b)*100.0;
		printf("进度：%lf%% \n",cb); 
	}
	
	fclose(fp);
	printf("\n");	
	etime = clock();
	printf("已将数据存储在2.xls，%ld 次模拟实验已完成 用时：%.3f s \n\n", b, (double)(etime - btime) / CLOCKS_PER_SEC);
	system("pause");
	return 0;
}